package com.kzd.gtgame.main.game


import androidx.recyclerview.widget.LinearLayoutManager
import com.kzd.gtgame.R
import com.kzd.gtgame.custom.setTransitionStatusE
import com.kzd.gtgame.jetpack.vm.GameViewModel
import com.kzd.gtgame.main.game.adapter.GameClassifyAdapter
import com.module.lemlin.http.Status

import com.module.lemlin.owner.OwnerViewModeFragment
import kotlinx.android.synthetic.main.fragment_game_classify.*
import kotlinx.android.synthetic.main.include_app_refresh.*

class GameClassifyFragment : OwnerViewModeFragment<GameViewModel>() {

    companion object {
        fun newInstance(): GameClassifyFragment {
            return GameClassifyFragment()
        }
    }

    private val mGameClassifyAdapter = GameClassifyAdapter()

    override fun viewModel(): GameViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.fragment_game_classify

    override fun initView() {
        rvAppRefreshBody.apply {
            layoutManager = LinearLayoutManager(requireContext())
        }.adapter = mGameClassifyAdapter

        mGameClassifyAdapter.setOnItemClickListener { _, _, position ->
            if (mGameClassifyAdapter.selectedPosition == position)
                return@setOnItemClickListener
            val itemData = mGameClassifyAdapter.data[position]
            viewModel.selectedClassifyIdLiveData.postValue(
                GameViewModel.GameUpdate(itemData.id)
            )
        }

        mSmartRefreshLayout.setOnRefreshListener {
            viewModel.gameClassify()
        }
    }

    override fun initData() {
        viewModel.gameClassify()
    }

    override fun initViewMode() {
        viewModel.gameClassifyLiveData.observe(viewLifecycleOwner, { response ->
            setTransitionStatusE(response,mGameClassifyAdapter,mSmartRefreshLayout,(false))
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
            if (data.isNullOrEmpty())
                return@observe
            mlMotionLayout.transitionToEnd()
            mSmartRefreshLayout.setEnableRefresh(false)
            mGameClassifyAdapter.setNewInstance(data)
            viewModel.selectedClassifyIdLiveData.postValue(
                GameViewModel.GameUpdate(data[0].id)
            )
        })

        viewModel.selectedClassifyIdLiveData.observe(viewLifecycleOwner, { update ->
            val index = mGameClassifyAdapter.data
                .indexOfLast { it.id == update.type }
            if (index == -1 || index == 0) {
                showFeaturedFragment1()
            } else {
                showListFragment1(update.type)
            }
            mGameClassifyAdapter.selectedPosition = index
        })

    }


    private fun showFeaturedFragment1() {
        childFragmentManager.beginTransaction().apply {
            replace(R.id.flGameClassifyBody, GameFeaturedFragment.newInstance())
        }.commit()
    }

    private fun showListFragment1(type:String) {
        childFragmentManager.beginTransaction().apply {
            replace(R.id.flGameClassifyBody, GameListFragment.newInstance(type))
        }.commit()
    }
}